package com.example.tamplate.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.tamplate.model.Category;
import com.example.tamplate.model.vo.Result;
import com.example.tamplate.service.impl.CategoryServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/category")
@Api(tags = "分类")
public class CategoryController {
    @Autowired
    public CategoryServiceImpl categoryService;

    @ApiOperation("添加")
    @PostMapping("/add")
    public Result addCategory(@Validated @RequestBody Category category, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            // 获取绑定结果中的字段错误信息
            FieldError fieldError = bindingResult.getFieldError();
            // 获取字段错误信息的错误消息
            String errorMessage = fieldError.getDefaultMessage();
            // 返回验证失败的结果，错误消息为错误提示信息
            return Result.fail(errorMessage);
        } else {
            categoryService.save(category);
            return Result.success(category);
        }
    }


    @ApiOperation("查询")
    @GetMapping("/list")
    public Result listCategory(@RequestParam(required = false) Integer pageNum, @RequestParam(required = false) Integer pageSize) {
        if (pageNum == null || pageSize == null) {
            List<Category> categoryList = categoryService.list();
            return Result.success(categoryList);
        }
        Page<Category> page = new Page<>(pageNum, pageSize);
        Page<Category> categoryPage = categoryService.page(page);
        return Result.success(categoryPage);
    }


    @ApiOperation("详情")
    @GetMapping("/detail/{id}")
    public Result<Category> detailCategory(@PathVariable Integer id) {
        Category category = categoryService.getById(id);
        return Result.success(category);
    }


    @ApiOperation("删除")
    @DeleteMapping("/delete/{id}")
    public Result deleteCategory(@PathVariable Integer id) {
        boolean removeById = categoryService.removeById(id);
        if (removeById) {
            return Result.success("删除成功");
        } else {
            return Result.fail("删除失败");
        }
    }


    @ApiOperation("修改")
    @PutMapping("/update/{id}")
    public Result<Category> updateCategory(@RequestBody Category category, @PathVariable Integer id) {
        category.setId(id);
        boolean updateById = categoryService.updateById(category);
        if (updateById) {
            // 检查是否成功修改了信息
            Category updatedCategory = categoryService.getById(id);
            if (updatedCategory != null) {
                return Result.success(updatedCategory);
            }
        }
        return Result.fail(category);
    }
}
